<template>
  <div class="commaudit">
    <div class="section">
      <h3>消息审核</h3>
    </div>
    <el-table
      :height="tabHeight"
      border
      style="width: 99%"
      align="center"
      :data="tableData"
      @selection-change="selectTable"
      this.loading="loading"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column prop="roId" label="ID" width="145"></el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="80"
      ></el-table-column>
      <el-table-column
        prop="cpName"
        label="所属CP"
        width="60"
      ></el-table-column>
      <el-table-column label="返利信息" width="80">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showdetail(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="auditRemark"
        label="查看/添加备注"
        class="borderRight"
      ></el-table-column>
      <el-table-column label width="50">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            plain
            @click="editRemark(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="审核操作" width="100">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="passNotes(scope.row)"
            size="mini"
            plain
            >通过</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="bottom">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="pagenationData.currentPage"
        :page-size="pagenationData.pageSize"
        :total="pagenationData.pageTotal"
        background
        layout="prev, pager, next"
        prev-text="上一页"
        next-text="下一页"
      ></el-pagination>
      <el-button type="primary" @click="passThrough">一键通过</el-button>
    </div>
    <el-dialog
      title="修改备注"
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form :model="editData">
        <el-form-item :label-width="formLabelWidth">
          <el-input
            autocomplete="off"
            type="textarea"
            v-model="editData.note"
            >{{ editData.note }}</el-input
          >
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="senEditremark">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查看返利信息 -->
    <ShowRebateDialog
      v-if="showRebateDialog.show"
      :isShow="showRebateDialog.show"
      :dialogRow="showRebateDialog.dialogRow"
      @closeDebateDialog="hideRebateDialog"
    ></ShowRebateDialog>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import ShowRebateDialog from "@/components/dialog/ShowRebateDialog";
import {
  getremarksData,
  remarkOrderPassBatch,
  remarkOrderPass,
  changeRemark,
} from "@/api/data";
export default {
  components: {
    ShowRebateDialog,
  },
  inject: ["reload"],
  data() {
    return {
      tabHeight: window.innerHeight - 200,
      tableData: [],
      loading: true,
      pagenationData: {
        pageTotal: 0,
        currentPage: 1,
        pageSize: 20,
      },
      dialogFormVisible: false,
      formLabelWidth: "120px",
      editData: {
        note: "",
      },
      roId: "",
      showRebateDialog: {
        show: false,
        dialogRow: {},
      },
      multipleSelection: [],
      pageData: {
        page: 1,
        limit: 20,
      },
      search: {},
    };
  },
  mounted() {
    this.getremarkList();
  },
  methods: {
    searchData(search) {
      this.pagenationData.currentPage = 1;
      this.search = search;
      this.search.page = 1;
      this.searchList();
    },
    // 获取备注审核数据
    getremarkList() {
      const para = Object.assign({}, this.pageData);
      getremarksData(para).then((res) => {
        this.loading = false;
        this.pagenationData.pageTotal = res.data.count;
        this.tableData = res.data.list;
      });
    },
    // 搜索数据
    searchList() {
      this.loading = true;
      const para = Object.assign({}, this.search);
      getremarksData(para).then((res) => {
        this.loading = false;
        this.pagenationData.pageTotal = res.data.count;
        this.tableData = res.data.list;
      });
    },
    editRemark(row) {
      this.roId = row.roId;
      this.editData.note = row.auditRemark;
      this.dialogFormVisible = true;
    },
    // 行内通过
    passNotes(row) {
      remarkOrderPass({ roId: row.roId }).then((res) => {
        let result = res.data.result;
        if (result == 2) {
          this.$message({
            type: "success",
            message: "操作成功",
          });
          setTimeout(() => {
            this.reload();
          }, 20);
        } else {
          this.$message({
            type: "warning",
            message: "操作失败",
          });
        }
      });
    },
    showdetail(row) {
      //返利信息弹框显示
      this.showRebateDialog.dialogRow = row; //将行内信息存储，方便弹框显示
      this.showRebateDialog.show = true;
    },
    hideRebateDialog() {
      //关闭查看返利
      this.showRebateDialog.show = false;
    },
    // 上下分页
    handleCurrentChange(val) {
      this.pageData.page = val;
      this.search.page = val;
      this.searchList();
    },
    // 当用户手动勾选数据行的 Checkbox 时触发的事件
    selectTable(val, row) {
      this.multipleSelection = val;
    },
    //一键通过
    passThrough() {
      let len = this.multipleSelection.length;
      if (len === 0) {
        this.$message({
          type: "warning",
          message: "请至少选择一项！",
        });
      } else {
        this.$confirm(`确定一键审核选中的 ${len} 条数据吗？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          var roIdArr = [];
          this.multipleSelection.forEach((item) => {
            roIdArr.push(item.roId);
          });
          let roIdStr = roIdArr.join(",");
          remarkOrderPassBatch({ roIds: roIdStr }).then((res) => {
            let result = res.data.result;
            if (result == 2) {
              this.$message({
                type: "success",
                message: "操作成功",
              });
              setTimeout(() => {
                this.reload();
              }, 20);
            } else {
              this.$message({
                type: "warning",
                message: "操作失败",
              });
            }
          });
        });
      }
    },
    // 备注修改
    senEditremark(row) {
      let param = {
        roId: this.roId,
        remark: this.editData.note,
      };

      changeRemark(param).then((res) => {
        let result = res.data.result;
        if (result == 2) {
          this.$message({
            type: "success",
            message: "操作成功",
          });
          setTimeout(() => {
            this.reload();
          }, 20);
        } else {
          this.$message({
            type: "warning",
            message: "操作失败",
          });
        }
      });
      this.dialogFormVisible = false;
    },
  },
};
</script>
<style lang="scss">
.commaudit {
  .el-icon-circle-check {
    font-size: 18px;
  }
  .pass.el-icon-circle-check {
    color: #67c23a;
  }
  .cell > span {
    color: #de4a63;
    margin-left: 5px;
  }
  .cell > * {
    vertical-align: middle;
  }
  .el-table {
    .el-button--primary {
      font-size: 12px;
      padding: 5px;
    }
  }
  .el-table_1_column_4 {
    border-right: 0;
    text-align: left;
  }
  .el-form-item__content {
    margin-left: 0 !important;
  }
  .el-dialog {
    .el-button {
      padding: 10px 20px;
    }
  }
  .el-textarea {
    textarea {
      height: 100px;
    }
  }
}
</style>